﻿using System;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class CFMStateItem : CFBase
    {
        private CFVO cfvo;
        private bool fEqual;
        private uint unused;

        public CFMStateItem(ConditionalFormatValueObject item)
        {
            this.fEqual = item.GreaterThanOrEqual;
            this.cfvo = new CFVO(item);
        }

        public CFMStateItem()
        {
        }

        public override void Read(BinaryReader reader)
        {
            cfvo = new CFVO();
            cfvo.Read(reader);
            fEqual = reader.ReadByte() != 0;
            unused = reader.ReadUInt32();
        }

        public override void Write(BinaryWriter writer)
        {
            cfvo.Write(writer);
            writer.Write((byte) (fEqual ? 1 : 0));
            writer.Write(unused);
        }

        public ConditionalFormatValueObject ToObject(BiffReaderWorksheetContext context)
        {
            var result = this.cfvo.ToCondition(context);
            result.GreaterThanOrEqual = this.fEqual;
            return result;
        }
    }
}